package api

import (
	goods "api-gateway/basic/goods_proto"
	"api-gateway/basic/inits"
	u "api-gateway/basic/user_proto"
	"api-gateway/handler/request"
	"api-gateway/handler/response"
	"context"
	"github.com/gin-gonic/gin"
)

// GoodsDetail
// @Summary      商品详情
// @Description  商品详情
// @Tags         商品模块
// @Accept       json
// @Produce      json
// @Param        goods  query      request.GoodsDetailReq  true  "商品参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Router       /goods/goodsDetail [get]
func GoodsDetail(c *gin.Context) {
	var req request.GoodsDetailReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	goodsDetail, err := inits.GoodsClient.GoodsDetail(c, &goods.GoodsDetailReq{GoodsId: req.GoodsId})
	if err != nil {
		response.Error(c, 500, "服务端错误", err.Error())
		return
	}
	response.Success(c, "商品详情展示成功", goodsDetail.GoodsDetail)
}

// GoodsDetail
// @Summary      店铺详情
// @Description  店铺详情
// @Tags         商品模块
// @Accept       json
// @Produce      json
// @Param        goods  query      request.ShopDetailReq  true  "商品参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Router       /goods/shopDetail [get]
func ShopDetail(c *gin.Context) {
	var req request.ShopDetailReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	shopDetail, err := inits.GoodsClient.ShopDetail(c, &goods.ShopDetailReq{GoodsId: req.GoodsId})
	if err != nil {
		response.Error(c, 500, "服务端错误", err.Error())
		return
	}
	response.Success(c, "商铺详情展示成功", shopDetail.ShopDetail)
}

// GoodsPush
// @Summary      商品推荐
// @Description  商品推荐
// @Tags         商品模块
// @Accept       json
// @Produce      json
// @Param        goods  query      request.GoodsPushReq  true  "商品参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Router       /goods/goodsPush [get]
func GoodsPush(c *gin.Context) {
	var req request.GoodsPushReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	goodsPush, err := inits.GoodsClient.GoodsPush(c, &goods.GoodsPushReq{})
	if err != nil {
		response.Error(c, 500, "服务端错误", err.Error())
		return
	}
	response.Success(c, "商品推荐成功", goodsPush.List)
}

// ProductCommentsAdd
// @Summary      一级评论添加
// @Description  一级评论添加
// @Tags         商品模块
// @Accept       json
// @Produce      json
// @Param        goods  body     request.ProductCommentsAdd  true  "商品参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /goods/productCommentsAdd [post]
func ProductCommentsAdd(c *gin.Context) {
	userId := c.GetUint("userId")
	var req request.ProductCommentsAdd
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}

	add, err := inits.UserClient.ProductCommentsAdd(context.Background(), &u.ProductCommentsAddReq{
		Id:        req.Id,
		ProductId: req.ProductId,
		UserId:    uint64(userId),
		Content:   req.Content,
		Rating:    req.Rating,
	})
	if err != nil {
		response.Error(c, 400, err.Error(), nil)
		return
	}
	response.Success(c, "一级评论添加成功", add.Id)
}

// CommentReplyAdd
// @Summary      二级评论添加
// @Description  二级评论添加
// @Tags         商品模块
// @Accept       json
// @Produce      json
// @Param        goods  body     request.CommentReplyAdd  true  "商品参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /goods/commentReplyAdd [post]
func CommentReplyAdd(c *gin.Context) {
	userId := c.GetUint("userId")
	var req request.CommentReplyAdd
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}

	add, err := inits.UserClient.CommentReplyAdd(context.Background(), &u.CommentReplyAddReq{
		Id:        req.Id,
		CommentId: req.CommentId,
		UserId:    uint64(userId),
		Content:   req.Content,
	})
	if err != nil {
		response.Error(c, 400, err.Error(), nil)
		return
	}
	response.Success(c, "二级评论添加成功", add.Id)
}

// CommentFind
// @Summary      评论展示
// @Description  评论展示
// @Tags         商品模块
// @Accept       json
// @Produce      json
// @Param        goods   query    request.CommentFind  true  "商品参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Router       /goods/commentFind [get]
func CommentFind(c *gin.Context) {
	var req request.CommentFind
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}

	id, err := inits.UserClient.GetCommentsByProductId(context.Background(), &u.GetCommentsByProductIdReq{
		ProductId: req.ProductId,
	})
	if err != nil {
		response.Error(c, 400, err.Error(), nil)
		return
	}
	response.Success(c, "评论添加成功", id.Comments)

}
func PopUpAd(c *gin.Context) {
	var req request.GetUserId
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	res, err := inits.GoodsClient.PopUpAd(c, &goods.PopUpAdReq{UserId: req.UserId})
	if err != nil {
		response.Error(c, 400, err.Error(), nil)
		return
	}
	response.Success(c, "评论添加成功", res)
}
